package com.shengyuan.manage_os.ui;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.base.NetworkActivity;
import com.shengyuan.manage_os.utils.DataSharedPress;
import com.shengyuan.manage_os.utils.EquipParam;
import com.shengyuan.manage_os.utils.ViewTools;

import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * Created by Administrator on 2016/11/18.
 */

public class SendDynaActivity extends NetworkActivity implements View.OnClickListener {

    private EditText editText;
    private TextView tv_check,image_select,tv_send,tv_tab;
    private ArrayList<String> mSelectPath = new ArrayList<>();
    private DataSharedPress sharedPress;
//    private MyGridView gridView;
    private ImageView[] imageViews = new ImageView[6];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);//设置状态栏为透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.send_layout);
        EquipParam.init(this);
        sharedPress = DataSharedPress.getSharedPress(this);
        editText = (EditText) findViewById(R.id.ed_content);
        tv_check = (TextView) findViewById(R.id.tv_check);
        tv_send = (TextView) findViewById(R.id.tv_send);
        tv_tab = (TextView) findViewById(R.id.tv_tab);
        tv_tab.setOnClickListener(this);
        image_select = (TextView) findViewById(R.id.image_select);
        imageViews[0] = (ImageView) findViewById(R.id.image1);
        imageViews[1] = (ImageView) findViewById(R.id.image2);
        imageViews[2] = (ImageView) findViewById(R.id.image3);
        imageViews[3] = (ImageView) findViewById(R.id.image4);
        imageViews[4] = (ImageView) findViewById(R.id.image5);
        imageViews[5] = (ImageView) findViewById(R.id.image6);
//        gridView = (MyGridView) findViewById(R.id.fabaio_tupian);
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();//获取焦点 光标出现
        tv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                ViewTools.showSetting(SendDynaActivity.this,view,280,350,2,null);
            }
        });
        image_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SendDynaActivity.this, MultiImageSelectorActivity.class);
                // 是否显示拍摄图片
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
                // 最大可选择图片数量
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 6);
                // 选择模式
                intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
                // 默认选择
                if (mSelectPath != null && mSelectPath.size() > 0) {
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
                }
                startActivityForResult(intent, 100);
            }
        });

       editText.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {

           }

           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {
           }

           @Override
           public void afterTextChanged(Editable s) {
               if("".equals(s.toString().trim())){
                   tv_send.setTextColor(SendDynaActivity.this.getResources().getColor(R.color.color_background));
                   tv_send.setBackgroundResource(R.drawable.shapeselect_hui);
               }else {
                   tv_send.setTextColor(SendDynaActivity.this.getResources().getColor(R.color.color_theme));
                   tv_send.setBackgroundResource(R.drawable.shapeselect_blue);
               }
           }
       });
        editText.setText(sharedPress.getString("text"));
    }

    ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data1) {
        super.onActivityResult(requestCode, resultCode, data1);
        if (requestCode == 100) {
            if (resultCode == RESULT_OK) {
                mSelectPath = data1.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);

                bitmaps.clear();

                for (int j = 0; j < mSelectPath.size(); j++) {
                    String p = mSelectPath.get(j);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    BitmapFactory.decodeFile(p, options);
                    options.inJustDecodeBounds = true;
                    int outHeight = options.outHeight;
                    int outWidth = options.outWidth;
                    System.out.println("######==mSelectPath===" + p);
                    int samle;
                    if (outWidth > outHeight)
                        samle = (int)(outWidth / EquipParam.getWindowWidth() * 3.3);
                    else
                        samle = (int)(outHeight / EquipParam.getWindowWidth() * 3.3);

                    options.inJustDecodeBounds = false;
                    if (samle > 1)
                        options.inSampleSize = samle;
                    else
                        options.inSampleSize = 1;
                    Bitmap bm1 = BitmapFactory.decodeFile(p, options);
                    bitmaps.add(bm1);

                }
                setGviewData(imageViews,bitmaps,mSelectPath,R.layout.grid_item_image);

            }
        }
    }

    public void setGviewData(ImageView[] imageViews, final List<Bitmap> data,final ArrayList<String> imagePath, int resource){
        //配置适配器
//        gridView.setAdapter(new GridViewAdapter(data,context));
//
//        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> arg0, View view, int arg2, long l) {
//
//            }
//        });
//        Toast.makeText(context,""+gridView.getAdapter().getCount(),Toast.LENGTH_LONG).show();
        int count = data.size();
//        ViewGroup.LayoutParams params = gridView.getLayoutParams();
//        View view = gridView.getAdapter().getView(0,null,null);
//        LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) view.getLayoutParams();
//        params1.height = EquipParam.getWindowWidth()/3-30;
//        view.setLayoutParams(params1);
//        if(count > 0 && count < 4){
//            params.height = EquipParam.getWindowWidth()/3-30;
//        }else {
//            params.height = EquipParam.getWindowWidth()/3*2-30;
//        }
//        gridView.setLayoutParams(params);


        View view = LayoutInflater.from(this).inflate(resource,null);
        LinearLayout linearLayout1 = (LinearLayout) view.findViewById(R.id.ly_image_1);
        LinearLayout linearLayout2 = (LinearLayout) view.findViewById(R.id.ly_image_2);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(EquipParam.getWindowWidth()/3-25, EquipParam.getWindowWidth()/3);
        layoutParams.setMargins(15,10,5,10);
        for(int i = 0; i< 6; i++){
            imageViews[i].setImageBitmap(null);
            imageViews[i].setOnClickListener(this);
            imageViews[i].setVisibility(View.GONE);
        }
        int i = 0;
        if (count > 0 ){
            linearLayout1.setVisibility(View.VISIBLE);
            for (Bitmap bitmap : data){
                if (i > 2)
                    break;
                imageViews[i].setVisibility(View.VISIBLE);
                imageViews[i].setImageBitmap(bitmap);
                imageViews[i].setLayoutParams(layoutParams);
                i++;
            }
            if(count > 3){
                linearLayout2.setVisibility(View.VISIBLE);
                for (i = 3;i < data.size();i++){
                    imageViews[i].setVisibility(View.VISIBLE);
                    imageViews[i].setImageBitmap(data.get(i));
                    imageViews[i].setLayoutParams(layoutParams);
                }
            }
        }


    }

    @Override
    protected void onStop() {
        super.onStop();
        sharedPress.putString("text",editText.getText().toString());
    }

    @Override
    public void onClick(View v) {
        String path;
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.image1:
                path = mSelectPath.get(0);
                break;
            case R.id.image2:
                path = mSelectPath.get(1);
                break;
            case R.id.image3:
                path = mSelectPath.get(2);
                break;
            case R.id.image4:
                path = mSelectPath.get(3);
                break;
            case R.id.image5:
                path = mSelectPath.get(4);
                break;
            case R.id.image6:
                path = mSelectPath.get(5);
                break;
            case R.id.tv_tab:
                editText.setText("");
                path = "";
                finish();
                break;
            default:
                path = "";
        }
        if(!"".equals(path)){
            intent.setClass(SendDynaActivity.this,ImagePreviewActivity.class);
            intent.putExtra("path",path);
            startActivity(intent);
        }

    }
}
